//
//  Layout.m
//  UI豆瓣2015到2016跨年作业
//
//  Created by LHY on 16/1/4.
//  Copyright © 2016年 Lanou 32 李昊宇. All rights reserved.
//

#import "Layout.h"

@interface Layout ()

@property (nonatomic , assign) NSUInteger numberOfItems;
@property (nonatomic , strong) NSMutableArray *columnHeights;
@property (nonatomic , strong) NSMutableArray *itemAtrributes;
@property (nonatomic , assign) CGFloat datalX;
@property (nonatomic , assign) CGFloat datalY;
@property (nonatomic , assign) NSInteger shortestIndex;
- (NSInteger)p_indexForLongestColumn;
- (NSInteger)p_indexForShortestColumn;

@end



@implementation Layout

- (NSMutableArray *)columnHeights
{

    if (!_columnHeights) {
        self.columnHeights = [NSMutableArray array];
    }
    return _columnHeights;
}


- (NSMutableArray *)itemAtrributes
{

    if (!_itemAtrributes) {
        self.itemAtrributes = [NSMutableArray array];
    }

    return _itemAtrributes;
}

- (NSInteger)p_indexForLongestColumn
{
    NSInteger longestIndex = 0;
    CGFloat longestHight = 0;
    for (int i = 0; i < self.numberOfItems; i++) {
        CGFloat currentHeight = [self.columnHeights[i]floatValue ];
        if (currentHeight > longestHight) {
            longestHight = currentHeight;
            longestHight = i;
        }
    }

    return longestIndex;
}

- (NSInteger)p_indexForShortestColumn {
    NSInteger shortestIndex = 0;
    CGFloat shortestHeight = MAXFLOAT;
    for (int i = 0; i < self.numberOfColumns; i++) {
        CGFloat currentHeight = [self.columnHeights[i]floatValue];
        if (currentHeight < shortestHeight) {
            shortestHeight = currentHeight;
            shortestIndex = i;
        }
    }
    return shortestIndex;


}


- (void)addHeightWithColumns
{
    for (int i = 0; i < self.numberOfColumns; i++) {
        self.columnHeights[i] = @(self.sectionLnsets.top);
    }



}

- (void)searchShortColumns{

    _shortestIndex = [self p_indexForShortestColumn];
    CGFloat shortestH = [self.columnHeights[_shortestIndex] floatValue];
    self.datalX = self.sectionLnsets.left + (self.itemSize.width + self.insertItemSpacing) * _shortestIndex;
    self.datalY = shortestH + self.insertItemSpacing;


}


- (void)setFrame: (NSIndexPath *)indexPath
{
    UICollectionViewLayoutAttributes *layoutArr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    CGFloat itemHeight = 0;
    if (_delegate && [_delegate respondsToSelector:@selector(heightForItemIndexPath:)]) {
        itemHeight = [_delegate heightForItemIndexPath:indexPath];
    }
    
    layoutArr.frame = CGRectMake(_datalX, _datalY, self.itemSize.width, itemHeight);
    [self.itemAtrributes addObject:layoutArr];
    self.columnHeights[_shortestIndex] = @(_datalY + itemHeight);
    
    



}




- (void)prepareLayout {
    [super prepareLayout];
    self.numberOfItems = [self.collectionView numberOfItemsInSection:0];
    for (int i = 0; i < self.numberOfColumns; i++) {
        [self searchShortColumns];
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        [self setFrame:indexPath];
    }



}


- (CGSize)collectionViewContentSize {

    NSInteger longerstIndex = [self p_indexForShortestColumn];
    CGFloat longestH = [self.columnHeights[longerstIndex]floatValue];
    CGSize contentSize = self.collectionView.frame.size;
    contentSize.height = longestH + self.sectionLnsets.bottom;
    return contentSize;
    
    
    
}

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    return self.itemAtrributes;


}






@end
